//
//  DataCollectLogList.m
//  MarketDataColloctor
//
//  Created by jiro on 10/03/09.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "DataCollectLogList.h"
#import "DataCollectLog.h"


@implementation DataCollectLogList

+ (id) dataCollectLogListWithPgResult: (PGresult*) res
{
	DataCollectLogList* ret = [ [ DataCollectLogList alloc ] init ];
	int rowCount = PQntuples( res );
	int columnCount = PQnfields( res );
	for ( int i = 0; i < rowCount; i++ )
	{
		NSMutableArray* values = [ [ NSMutableArray alloc ] init ];
		for ( int j = 0; j < columnCount; j++ )
		{
			[ values addObject: [ NSString stringWithCString: PQgetvalue( res, i, j ) encoding: NSUTF8StringEncoding ] ];
		}
		[ ret addObject: [ DataCollectLog dataCollectLogWithDbValues: values ] ];
	}
	return ret;
}

//- (NSDate*) latestDateForCode: (NSString*) code0
//{
//	NSEnumerator* e = [ self objectEnumerator ];
//	DataCollectLog* log;
//	while ( log = [ e nextObject ] )
//	{
//		if ( [ log.code0 isEqualToString: code ] )
//		{
//			return log.latestDate;
//		}
//	}
//	return nil;
//}

- (NSString*) sqlInsertStatement
{
	NSMutableString* sql = [ NSMutableString string ];
	[ sql appendFormat: @"insert into %@ values\n", [ DataCollectLog tableName ] ];
	NSEnumerator* e = [ self objectEnumerator ];
	DataCollectLog* value;

	BOOL firstValue = YES;
	while ( value = [ e nextObject ] )
	{
		if ( firstValue ) {
			firstValue = NO;
		} else {
			[ sql appendString: @", " ];
		}
		[ sql appendFormat: @"%@\n", [ value queryInsertValue ] ];
	}
	[ sql appendString: @";\n" ];	
	return sql;	
}


@end
